﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using XYAC.Framework.MonitorTireMaterialOnline.Entities;
using XYAC.Framework.MonitorTireMaterialOnline.Extensions;

namespace XYAC.Framework.MonitorTireMaterialOnline.Context
{
    public partial class DatabaseContext : DbContext
    {
        public DatabaseContext()
        {
        }

        public DatabaseContext(IOptionsMonitor<AppConfig> moduleConfig)
            : base()
        {
            AppConfig = moduleConfig;
        }

        public virtual DbSet<PowerList> PowerList { get; set; } = null!;
        public virtual DbSet<PowerRole> PowerRole { get; set; } = null!;
        public virtual DbSet<RoleList> RoleList { get; set; } = null!;
        public virtual DbSet<SpecInfo> SpecInfo { get; set; } = null!;
        public virtual DbSet<UserList> UserList { get; set; } = null!;
        public IOptionsMonitor<AppConfig> AppConfig { get; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263.
                //optionsBuilder.UseJet("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=./LocalDB/CArea/DataBase.mdb");
                optionsBuilder.UseJet(AppConfig?.CurrentValue?.AccessCAreaBaseConnectionString);
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<PowerList>(entity =>
            {
                entity.HasNoKey();

                entity.ToTable("PowerList");

                entity.Property(e => e.ID)
                    .HasColumnType("counter")
                    .ValueGeneratedOnAdd()
                    .HasColumnName("ID");

                entity.Property(e => e.PowerName).HasMaxLength(20);
            });

            modelBuilder.Entity<PowerRole>(entity =>
            {
                entity.HasNoKey();

                entity.ToTable("PowerRole");

                entity.HasIndex(e => e.PowerId, "PowerID");

                entity.HasIndex(e => e.RoleId, "RoleID");

                entity.Property(e => e.PowerId)
                    .HasColumnName("PowerID")
                    .HasDefaultValueSql("0");

                entity.Property(e => e.RoleId)
                    .HasColumnName("RoleID")
                    .HasDefaultValueSql("0");
            });

            modelBuilder.Entity<RoleList>(entity =>
            {
                entity.ToTable("RoleList");

                entity.HasIndex(e => e.Id, "ID");

                entity.Property(e => e.Id)
                    .HasColumnType("counter")
                    .HasColumnName("ID");

                entity.Property(e => e.Role).HasMaxLength(10);
            });

            modelBuilder.Entity<SpecInfo>(entity =>
            {
                entity.ToTable("SpecInfo");

                entity.HasIndex(e => e.Id, "ID")
                    .IsUnique();

                entity.HasIndex(e => e.ProfileName, "ProfileName")
                    .IsUnique();

                entity.Property(e => e.Id)
                    .HasColumnType("counter")
                    .HasColumnName("ID");

                entity.Property(e => e.AreaTol).HasDefaultValueSql("0");

                entity.Property(e => e.DateTimePicker).HasMaxLength(50);

                entity.Property(e => e.FindMidType).HasDefaultValueSql("0");

                entity.Property(e => e.Gjd).HasColumnName("GJD");

                entity.Property(e => e.Jiao)
                    .HasMaxLength(50)
                    .HasColumnName("jiao");

                entity.Property(e => e.NominalType).HasDefaultValueSql("0");

                entity.Property(e => e.ProfileName).HasMaxLength(50);

                entity.Property(e => e.ResultPercent).HasDefaultValueSql("0");

                entity.Property(e => e.RimHeight).HasDefaultValueSql("0");

                entity.Property(e => e.RimTol).HasDefaultValueSql("0");

                entity.Property(e => e.ShoulderHeight).HasDefaultValueSql("0");

                entity.Property(e => e.ShoulderHeightTol).HasDefaultValueSql("0");

                entity.Property(e => e.ShoulderWidth).HasDefaultValueSql("0");

                entity.Property(e => e.ShoulderWidthTol).HasDefaultValueSql("0");

                entity.Property(e => e.TextNo).HasMaxLength(50);

                entity.Property(e => e.TotalWidth).HasDefaultValueSql("0");

                entity.Property(e => e.WeightAlmDown).HasDefaultValueSql("0");

                entity.Property(e => e.WeightAlmUp).HasDefaultValueSql("0");

                entity.Property(e => e.WeightSd)
                    .HasColumnName("WeightSD")
                    .HasDefaultValueSql("0");

                entity.Property(e => e.WeightTolDown).HasDefaultValueSql("0");

                entity.Property(e => e.WeightTolUp).HasDefaultValueSql("0");

                entity.Property(e => e.WidthTol).HasDefaultValueSql("0");
            });


            modelBuilder.Entity<UserList>(entity =>
            {
                entity.ToTable("UserList");

                entity.HasIndex(e => e.ID, "ID");

                entity.Property(e => e.ID)
                    .HasColumnType("counter")
                    .HasColumnName("ID");

                entity.Property(e => e.FileSavePath).HasMaxLength(50);

                entity.Property(e => e.IsDeaul).HasColumnType("bit");

                entity.Property(e => e.Pwd).HasMaxLength(20);

                entity.Property(e => e.RoleId)
                    .HasColumnName("RoleID")
                    .HasDefaultValueSql("0");

                entity.Property(e => e.UserName).HasMaxLength(20);
            });

            OnModelCreatingPartial(modelBuilder);
        }

        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
    }
}
